using System;
using System.Collections;
using System.Data;
using System.Collections.Generic;
using System.Diagnostics;

namespace FreeFlow.Administration
{
	/// <summary>
	/// A collection of procedure versions.
	/// </summary>
	public class ProcedureVersionCollection : VersionCollection
	{
    /// <summary>
    /// Initializes a new instance of the <see cref="VersionCollection"/> class for the specified procedure or library.
    /// </summary>
    /// <param name="server">The server.</param>
    /// <param name="procedureName">Name of the procedure or library.</param>
    public ProcedureVersionCollection(Server server, string procedureName) : base(server, procedureName)
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="VersionCollection"/> class for the specified version of a library. 
    /// The collection will contain the versions of the procedures that use this version of the library.
    /// </summary>
    /// <param name="server">The server.</param>
    /// <param name="libraryName">Name of the library.</param>
    /// <param name="libraryVersion">The version.</param>
    public ProcedureVersionCollection(Server server, string libraryName, int libraryVersion) : base(server, libraryName)
    {
      this.procedureName = libraryName;
      this.procedureVersion = libraryVersion;
    }

    internal override void ReadCount()
    {
      if (procedureVersion == 0)
      {
        count = Convert.ToInt32(Server.ExecuteScalar(
          string.Format("SELECT COUNT(*) FROM eProcedure WHERE eProcedureName='{0}'", SqlEscape(procedureName))));
      }
      else if (Server.SchemaVersion >= SchemaVersion.Version70)
      {
        count = Convert.ToInt32(Server.ExecuteScalar(
          string.Format("SELECT COUNT(*) FROM eProcedureLibrary, eProcedure WHERE " +
          "eProcedureLibrary.eLibraryName = '{0}' and eProcedureLibrary.eLibraryVersion = {1} AND " +
          "eProcedureLibrary.eProcedureName = eProcedure.eProcedureName AND " +
          "eProcedureLibrary.eProcedureVersion = eProcedure.eVersion", SqlEscape(procedureName), procedureVersion)));
      }
    }

    internal override void ReadVersions()
    {
      if (versionsList == null)
      {
        versionsList = new List<Version>();
        if (procedureVersion == 0)
        {
          using (IDataReader reader = Server.ExecuteReader(
                   string.Format("SELECT eProcedureName, eProcedureType, eVersion, eProcessOwner, eworkVersion, eLoadedTime, eNotes " +
                   "FROM eProcedure WHERE eProcedureName = '{0}' ORDER BY eVersion DESC", SqlEscape(procedureName))))
          {
            while (reader.Read()) 
            {
              Version version = new ProcedureVersion(Server, reader);
              versionsList.Add(version);
            }  
          }
        }
        else if (Server.SchemaVersion >= SchemaVersion.Version70)
        {
          // read versions
          using (IDataReader reader = Server.ExecuteReader(
                   string.Format("SELECT eProcedure.* FROM eProcedureLibrary, eProcedure WHERE " + 
                   "eProcedureLibrary.eLibraryName = '{0}' and eProcedureLibrary.eLibraryVersion = {1} AND " +
                   "eProcedureLibrary.eProcedureName = eProcedure.eProcedureName AND " +
                   "eProcedureLibrary.eProcedureVersion = eProcedure.eVersion ORDER BY eVersion DESC", SqlEscape(procedureName), procedureVersion)))
          {
            while (reader.Read()) 
            {
              Version version = new Version(Server, reader);
              versionsList.Add(version);
            }  
          }
        }
      }
    }
	}
}
